// pages/creatVR/creatVR.js
const db = wx.cloud.database()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        VR:[],
        land:{},
        prePath:"",
        preList:[]
    },
    afterReadVR(event) {
        const { file } = event.detail;
        this.setData({
            VR:file
        })
    },
   async deleteVR(event){
        this.setData({
            VR:[]
        })
        let{preList} = this.data;
        if(preList.length != 0){
            await this.removeVR(preList) 
        }
    },
    onLoad:function(options){
        var that = this;
        var id = options.id;
        if(id.length != 0){
        db.collection('land').doc(id).get().then(res=>{
            that.setData({
                land:res.data
            })
        })
        }
    },
     onunload:async function(){
        let{preList} = this.data;
        if(preList.length != 0){
            await this.removeVR(preList) 
        }
    },

    async check()
    {
        var vr = this.data.land.VR;
        if(vr.length==0){
            wx.showToast({
              title: '你还未上传全景图，无法查看！',
              icon:"none"
            })
        }else{
            var path = await this.path(vr);
            wx.navigateTo({
            url: '/pages/vr/vr?id='+path
            })
        }
    },

    async uploadVR(imgPath,landid,pre){
        var user = wx.getStorageSync('userFlag').user
        var cloudPath="";  
        if(!pre){
           cloudPath  = user._id +'/'+landid+new Date().getTime()+'VR.png';
        }else{
            cloudPath = user._id+'/'+new Date().getTime()+'previewVR.png';
        }
        var id;
        await wx.cloud.uploadFile({
            cloudPath:cloudPath ,
            filePath: imgPath // 文件路径
        }).then(res => {
            id=res.fileID
        }).catch(error => {
        })
        return id;
    },

    async upload(){
        var that = this;
        var {VR} = that.data;
        var landid = that.data.land._id;
        var landvr = that.data.land.VR;
        var {preList} = that.data;   
        if(VR[0]){ 
                wx.showLoading({
                    title: '加载中',
                })
                if(preList.length!=0){
                    await that.removeVR(preList);
                    that.setData({
                        preList:[]
                    })
                }
                var vr = await that.uploadVR(VR[0].url,landid,false);
                if(landvr){
                   await that.removeVR([landvr]);
                }
                await db.collection('land').doc(landid).update({
                    data:{
                        VR:vr
                    }
                }).then(res=>{
                    that.setData({
                        ['land.VR']:vr
                    }) 
                })
                wx.showToast({
                    title: '上传成功',
                })
                wx.navigateBack({
                  delta: 1,
                })
        }else{
            wx.showToast({
              title: '你还未选择图片',
              icon:"none"
            })
        }
    },

    //获取网络路径
    async path(fileid){
        var path="";
        await wx.cloud.getTempFileURL({
            fileList: [{
                fileID: fileid,
                maxAge: 60 * 10, 
              }]
        }).then(res=>{
            path = res.fileList[0].tempFileURL;
        })
        return path;
    },

    //预览
   async preview(){
    var {preList} = this.data;
    var that = this;
    var {VR} = this.data;
        if(VR[0]){ 
            wx.showLoading({
                title: '加载中',
            })
            //如果还存在测试图片，则删除；
            if(preList.length != 0){
                await that.removeVR(preList);
                that.setData({
                    preList:[]
                })
                preList=[];
            }
            var fileid = await this.uploadVR(VR[0].url,"",true);
            preList.push(fileid);
            
            that.setData({

                preList:preList
            })
            wx.hideLoading({
                success: (res) => {},
            })
            var path = await that.path(fileid);
            wx.navigateTo({
                url: '/pages/vr/vr?id='+path
            })
        }else{
            wx.showToast({
            title: '你还未选择图片',
            icon:"none"
            })
        }
    },

    async removeVR(VR){
        await  wx.cloud.deleteFile({
            fileList: VR
          }).then(res => {
          }).catch(error => {
          })
    },
})